package com.sti.platform.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.aisino.core.dao.BaseDao;
import com.aisino.tools.enums.PageEnum;
import com.aisino.tools.enums.QlEnum;
import com.aisino.tools.util.page.Page;
import com.aisino.tools.util.page.Pagination;
import com.sti.platform.dao.UserDao;
import com.sti.platform.model.Company;
import com.sti.platform.model.User;
import com.sti.platform.model.UserGroup;
import com.sti.platform.model.UserRole;

@Repository
public class UserDaoImpl extends BaseDao implements UserDao{

	@Override
	public List<User> queryUsersByCondition(String condition,Integer page,Integer row) {
		Page page1 = null;
		Page pageModel = new Pagination();
		pageModel.setPage(page);//当前页数
		pageModel.setRow(row);//每页多少条
		Integer total = this.queryUserSum();
		pageModel.setTotal(total);
		StringBuffer string = new StringBuffer();
		String sql = "SELECT u.*,GROUP_CONCAT(r.name) AS \"role\",c.name AS \"company\""
				+ " FROM sys_user u "
				+ " LEFT JOIN sys_user_role ur ON u.id = ur.user_id "
				+ " LEFT JOIN sys_role r ON ur.role_id = r.id "
				+ " LEFT JOIN sys_company c ON u.company_id = c.id";
		string.append(sql);
		if(!"".equals(condition) && condition != null){
			String s = " WHERE u.id = '" + condition + "' or u.real_name like '%" + condition+"%'"
					+ " or u.user_name like '%" + condition+"%' or c.name like '%" + condition+"%' ";
			string.append(s);
		} 
		string.append(" GROUP BY u.user_name");
		page1 = super.getByHibernateQuery(string.toString(),null,
				null, 1000, pageModel, User.class, QlEnum.SQL, PageEnum.PAGE_YES);
		
		@SuppressWarnings("unchecked")
		List<User> users = page1.getRecord();
		return users;
	}

	@Override
	public Map<String,Object> queryUserInfo(String userId) {
		String sql = "SELECT u.*,GROUP_CONCAT(r.id) as \"roleId\",GROUP_CONCAT(r.name) AS \"role\",GROUP_CONCAT(r.code) AS \"roleCode\","
				+ " c.fjd_id as \"fjdId\",c.id as \"companyId\",c.name AS \"company\",c.code AS \"comCode\",c.address as \"comAdd\","
				+ "	GROUP_CONCAT(g.id) as \"groupId\",GROUP_CONCAT(g.name) AS \"group\",GROUP_CONCAT(g.code) AS \"groupCode\""
				+ " FROM sys_user u "
				+ " LEFT JOIN sys_user_role ur ON u.id = ur.user_id "
				+ " LEFT JOIN sys_role r ON ur.role_id = r.id "
				+ " LEFT JOIN sys_company c ON u.company_id = c.id"
				+ " LEFT JOIN sys_user_group ug ON u.id = ug.user_id"
				+ " LEFT JOIN sys_group g ON ug.group_id = g.id "
				+ " WHERE u.id = '" + userId + "'"
				+ " GROUP BY u.user_name";
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> list = super.getBySql(sql,HashMap.class);
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public List<Map<String, Object>> queryRole() {
		String sql = "select * from sys_role where status = 0";
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> list = super.getBySql(sql);
		return list;
	}

	@Override
	public  List<Company> queryCompany() {
		String sql = "select * from sys_company ";
		@SuppressWarnings("unchecked")
		List<Company> list = super.getBySql(sql,Company.class);
		return list;
	}

	@Override
	public List<Map<String, Object>> queryGroup() {
		String sql = "select * from sys_group where status = 0";
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> list = super.getBySql(sql);
		return list;
	}

	@Override
	public List<UserGroup> queryGroup(String userID, String groupId) {
		String sql = "select * from sys_user_group where user_id = '"+userID+"' and group_id = '"+groupId+"'" ;
		@SuppressWarnings("unchecked")
		List<UserGroup> list = super.getBySql(sql, UserGroup.class);
		return list;
	}

	@Override
	public List<UserRole> queryRole(String userID, String roleId) {
		String sql = "select * from sys_user_role where user_id = '"+userID+"' and role_id = '"+roleId+"'" ;
		@SuppressWarnings("unchecked")
		List<UserRole> list = super.getBySql(sql,UserRole.class);
		return list;
	}

	@Override
	public List<UserGroup> queryGroup(String userID) {
		String sql = "select * from sys_user_group where user_id = '"+userID+"'" ;
		@SuppressWarnings("unchecked")
		List<UserGroup> list = super.getBySql(sql, UserGroup.class);
		return list;
	}

	@Override
	public List<UserRole> queryRole(String userID) {
		String sql = "select * from sys_user_role where user_id = '"+userID+"'" ;
		@SuppressWarnings("unchecked")
		List<UserRole> list = super.getBySql(sql,UserRole.class);
		return list;
	}

	@Override
	public Integer queryUserSum() {
		Integer sum = super.getTotalCount(" SELECT * FROM sys_user ", null, null, QlEnum.SQL);
		return sum;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> queryUser(String sql) {
		return super.getBySql(sql, User.class);
	}

}
